package cn.bdqn.witcity.controller;

import cn.bdqn.witcity.entity.Pay;
import cn.bdqn.witcity.entity.User;
import cn.bdqn.witcity.entity.vo.PersonInfo;
import cn.bdqn.witcity.mapper.PersonMapper;
import cn.bdqn.witcity.service.UserService;
import cn.bdqn.witcity.util.ResponseResult;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class PersonController {
    @Resource
    PersonMapper personMapper;
    @Resource
    UserService userService;

    @RequestMapping("/sys/paylist")
    public ResponseResult findAll() {
        List<PersonInfo> personInfos = personMapper.findAll();
        int count = personMapper.getCount();
        return new ResponseResult(0, "", personInfos, count);
    }

    @RequestMapping("/yezhu/payperson")
    public ResponseResult find(Authentication authentication) {
        User user = (User) authentication.getPrincipal();
        List<PersonInfo> personInfo = personMapper.findById(user.getId());
        return new ResponseResult(0, "", personInfo, 1);
    }

    @RequestMapping("/payWuYe")
    public ResponseResult payWuYe(Integer typeId, double money, Authentication authentication) {
        Pay pay = new Pay();
        User user = (User) authentication.getPrincipal();
        pay.setUid(user.getId());
        pay.setTypeId(typeId);
        pay.setMoney(money);
        int count = personMapper.updateYuer(pay);
        count += personMapper.addPay(pay);
        user.setMoney(user.getMoney()-money);
        count+=userService.updateMoney(user);
        if (count == 3) {
            return new ResponseResult(200, "", count);
        }
        return new ResponseResult(0, "", count);

    }
}
